#include <myfilelog.hxx>
#include <loghelper.hxx>

#include <cstdlib> // system()
#include <string.h>
#include <errno.h>
#include <sstream>
#include <cassert>
#include <iostream>
#include <curl/curl.h>
#include <utility.hxx>
#include <mymutex.hxx>
#include <macro.hxx>
#include "hvyattackworker.hxx"

using namespace std;
using namespace Utility;

HvyAttackWorker::HvyAttackWorker(int port, const string& cjh, const string& clxh, FileLog* filelog)
: AttackWorker(port, "", filelog),
_cjh(cjh),
_clxh(clxh),
_registered(false)
{
}

void HvyAttackWorker::Execute(Thread::Arg)
{
    stringstream body;
    string slrq;

    APPENDMSGEX("HvyAttackWorker #"<<Id()<<" started.", 0);

    int tick = 0;
    CURL* curl = NULL;
    while (!_is_shutdown_requested)
    {
        if (tick++ > 56)
        {
            // in case Autoer failed to send stopattack message.
            Shutdown();
        }

		APPENDMSGEX("HvyAttackWorker #"<<Id()<<" tick "<<tick<<".", 0);

        if (!_registered)
        {
            curl = Utility::NewCURL();
            int regres = Register(curl, body, slrq, _isattacking);
            if (regres)
            {
                APPENDMSGEX("HvyAttackWorker #"<<Id()<<" register @"<<_port<<" failed with #"<<regres<<"!!", 2);

                Utility::CleanupCurl(curl);
                curl = NULL;
                Yield();

                continue;
            }
            else
            {
                APPENDMSGEX("HvyAttackWorker #"<<Id()<<" register @"<<_port<<" succeeded!", 0);

                _registered = true;
            }
        }

        assert(!slrq.empty());

        int slrqres = Slrq(curl, body, slrq, _isattacking);

        if (0 != slrqres)
        {
            APPENDMSGEX("HvyAttackWorker #"<<Id()<<" Slrq @"<<_port<<" failed with error #"<<slrqres<<"!!", 2);

            _registered = false;  // re-register it.

            if (curl)
            {
                Utility::CleanupCurl(curl);
                curl = NULL;
            }
        }
        else
            APPENDMSGEX("HvyAttackWorker #"<<Id()<<" Slrq @"<<_port<<" succeeded", 1);
    }

    if (curl)
    {
        Utility::CleanupCurl(curl);
        curl = NULL;
    }

    APPENDMSGEX("HvyAttackWorker #"<<Id()<<" shutdowned.", 0);
}


int HvyAttackWorker::Register(CURL* curl, stringstream& body, string& slrq, bool& isattacking)
{
	const int MaxTry = 30;
	int tryCounter = 0;
	int performRes = 0;

	{
		const string method = "OptFZJG.aspx";
		const string url = Host + ":" + ItoStr(_port) +"/" + method;
		const string dataFields = "";

		while (isattacking && ++tryCounter < MaxTry && 0 != (performRes = Utility::PerformStep(curl, url, dataFields, body)) && CAN_RE_REQUEST(performRes))
		{
			//LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed errno #"<<performRes<<" at PerformStep 1 at try "<<tryCounter);
			Thread::SleepMs(100);
		}

		if (performRes)
		{
			LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed errno #"<<performRes<<" at PerformStep 1 finally");
			DumpFile(body, "dump_step_"+ItoStr(Id())+"_1.txt");
			return performRes;
		}
	}

	{
		const string method = "main.aspx";
		const string queryStr = string("?str=510100")
			+ "|%E5%B7%9DA" 
			+ "|%E6%88%90%E9%83%BD%E5%B8%82%E5%85%AC%E5%AE%89%E5%B1%80%E4%BA%A4%E9%80%9A%E8%AD%A6%E5%AF%9F%E6%94%AF%E9%98%9F%E8%BD%A6%E8%BE%86%E7%AE%A1%E7%90%86%E6%89%80";
		const string url = Host + ":" + ItoStr(_port) +"/" + method + queryStr;
		const string dataFields = "";

		tryCounter = 0;
		while (isattacking && ++tryCounter < MaxTry && 0 != (performRes = Utility::PerformStep(curl, url, dataFields, body)) && CAN_RE_REQUEST(performRes))
		{
			//LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed errno #"<<performRes<<" at PerformStep 2 at try "<<tryCounter);
			Thread::SleepMs(100);
		}

		if (performRes)
		{
			LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed errno #"<<performRes<<" at PerformStep 2 finally");
			DumpFile(body, "dump_step_"+ItoStr(Id())+"_2.txt");
			return performRes;
		}

	}

	{
		const string curPage = "main.aspx";
		const string nextPage = "FormPlateNoSelect.aspx";
		const string queryStr = "?str=510100%7c%u5dddA%7c%u6210%u90fd%u5e02%u516c%u5b89%u5c40%u4ea4%u901a%u8b66%u5bdf%u652f%u961f%u8f66%u8f86%u7ba1%u7406%u6240";
		const string dataFields = GetViewState(body)
			+ "&txtXhxmXx=+++++++++++++++%E5%B0%8F%E5%9E%8B%E6%B1%BD%E8%BD%A6%E5%8F%B7%E7%89%8C%E8%87%AA%E4%B8%BB%E7%BC%96%E5%8F%B7%E8%AF%B4%E6%98%8E%0D%0A++++%E6%AC%A2%E8%BF%8E%E6%82%A8%E4%BD%BF%E7%94%A8%E6%88%90%E9%83%BD%E5%B8%82%E5%85%AC%E5%AE%89%E5%B1%80%E4%BA%A4%E9%80%9A%E8%AD%A6%E5%AF%9F%E6%94%AF%E9%98%9F%E8%BD%A6%E8%BE%86%E7%AE%A1%E7%90%86%E6%89%80%E5%B0%8F%E5%9E%8B%E6%B1%BD%E8%BD%A6%E5%8F%B7%E7%89%8C%E8%87%AA%E4%B8%BB%E7%BC%96%E5%8F%B7%E7%B3%BB%E7%BB%9F%EF%BC%88%E4%BB%A5%E4%B8%8B%E7%AE%80%E7%A7%B0%22%E7%BC%96%E5%8F%B7%E7%B3%BB%E7%BB%9F%22%EF%BC%89%EF%BC%8C%E6%95%AC%E8%AF%B7%E4%BB%94%E7%BB%86%E9%98%85%E8%AF%BB%E4%BB%A5%E4%B8%8B%E6%8F%90%E7%A4%BA%EF%BC%8C%E6%8C%89%E7%85%A7%E8%A7%84%E5%88%99%E9%80%89%E6%8B%A9%E8%87%AA%E5%B7%B1%E5%BF%83%E4%BB%AA%E7%9A%84%E5%8F%B7%E7%89%8C%E3%80%82%0D%0A%E4%B8%80%E3%80%81%22%E8%87%AA%E4%B8%BB%E7%BC%96%E5%8F%B7%E7%B3%BB%E7%BB%9F%22%E9%80%82%E7%94%A8%E4%BA%8E%E5%9C%A8%E6%9C%AC%E5%B8%82%E7%94%B3%E8%AF%B7%E6%96%B0%E8%BD%A6%E6%B3%A8%E5%86%8C%E7%99%BB%E8%AE%B0%E7%9A%84%E5%B0%8F%E5%9E%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%89%E6%8B%A9%E5%8F%B7%E7%89%8C%E3%80%82%0D%0A%E5%A6%82%E6%82%A8%E5%B7%B2%E8%AE%A4%E7%9C%9F%E9%98%85%E8%AF%BB%E5%B9%B6%E5%90%8C%E6%84%8F%E4%BB%A5%E4%B8%8A%E8%A7%84%E5%88%99%EF%BC%8C%E8%AF%B7%E7%82%B9%E5%87%BB%22%E5%90%8C%E6%84%8F%22%E8%BF%9B%E5%85%A5%22%E7%BC%96%E5%8F%B7%E7%B3%BB%E7%BB%9F%22%EF%BC%8C%E5%B9%B6%E6%8C%89%E7%95%8C%E9%9D%A2%E7%9A%84%E6%8C%87%E7%A4%BA%E5%BD%95%E5%85%A5%E4%BF%A1%E6%81%AF%E3%80%82%E7%A5%9D%E6%82%A8%E8%83%BD%E5%A4%9F%E8%87%AA%E7%BC%96%E4%B8%AD%E4%B8%80%E5%89%AF%E8%87%AA%E5%B7%B1%E6%BB%A1%E6%84%8F%E7%9A%84%E5%8F%B7%E7%89%8C%EF%BC%81&butNext=++%E5%90%8C%E6%84%8F++"
			+ "&" + GetEventValidation(body);
		const string url = Host + ":" + ItoStr(_port) +"/" + curPage + queryStr;

		tryCounter = 0;
		while (isattacking && ++tryCounter<MaxTry && 0 != (performRes = Utility::PerformStep(curl, url, dataFields, body)) && CAN_RE_REQUEST(performRes))
		{
			//LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed errno #"<<performRes<<" at PerformStep 3 at try "<<tryCounter);
			Thread::SleepMs(100);
		}

		if (performRes)
		{
			LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed errno #"<<performRes<<" at PerformStep 3 finally");
			DumpFile(body, "dump_step_"+ItoStr(Id())+"_3.txt");
			return performRes;
		}
		if (!Utility::FileContain(body, nextPage))
		{

			LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed errno #"<<performRes<<" at PerformStep 3 finally");
			DumpFile(body, "dump_step_"+ItoStr(Id())+"_3_nonextpage.txt");
			return -1;
		}
	}

	{
		const string curPage = "FormPlateNoSelect.aspx";
		const string nextPage = "selSlrq_510100.aspx";
		const string dataFields = GetViewState(body)
			+ "&butNewVehPlateNO=%E5%B0%8F%E5%9E%8B%E6%B1%BD%E8%BD%A6%E6%B3%A8%E5%86%8C%E7%99%BB%E8%AE%B0%E8%87%AA%E4%B8%BB%E7%BC%96%E5%8F%B7"
			+ "&" + GetEventValidation(body);
		const string url = Host + ":" + ItoStr(_port) +"/" + curPage;

		tryCounter = 0;
		while (isattacking && ++tryCounter<MaxTry && 0 != (performRes = Utility::PerformStep(curl, url, dataFields, body)) && CAN_RE_REQUEST(performRes))
		{
			//LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed errno #"<<performRes<<" at PerformStep 4 at try "<<tryCounter);
			Thread::SleepMs(100);
		}

		if (performRes)
		{
			LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed errno #"<<performRes<<" at PerformStep 4 dataFields="<<dataFields);
			DumpFile(body, "dump_step_"+ItoStr(Id())+"_4.txt");
			return performRes;
		}
		if (!Utility::FileContain(body, nextPage))
		{

			LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed at PerformStep 4 no nextpage:dataFields="<<dataFields);
			DumpFile(body, "dump_step_"+ItoStr(Id())+"_4_nonextpage.txt");
			return -1;
		}
	}

	// 5
	{
		const string curPage = "selSlrq_510100.aspx";
		const string nextPage = "selSlrq_510100_1.aspx";

		const string dataFields = GetViewState(body) 
			+ "&tbCjh=" + _cjh 
			+ "&tbClxh=" + _clxh
			+ "&butSelect=+%E6%9F%A5+%E8%AF%A2+"
			+ "&" + GetEventValidation(body);
		const string url = Host + ":" + ItoStr(_port) +"/" + curPage;

		tryCounter = 0;
		while (isattacking && ++tryCounter<MaxTry && 0 != (performRes = Utility::PerformStep(curl, url, dataFields, body)) && CAN_RE_REQUEST(performRes))
		{
			//LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed errno #"<<performRes<<" at PerformStep 5 at try "<<tryCounter);
			Thread::SleepMs(100);
		}

		if (performRes)
		{
			LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed errno #"<<performRes<<" at PerformStep 5 dataFields="<<dataFields<<" finally");
			DumpFile(body, "dump_step"+Utility::ItoStr(Id())+"_5.txt");
			return performRes;
		}

		if (!Utility::FileContain(body, nextPage))
		{

			LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed at PerformStep 5 no nextpage:dataFields="<<dataFields);
			DumpFile(body, "dump_step_"+ItoStr(Id())+"_5_nonextpage.txt");
			return -1;
		}
	}

	string selected;
	std::map<std::string, std::string> slrqs;
	ExtractSelectOptions(body, "dSlrq", slrqs, selected);

	if (slrqs.empty())
	{
		LOGWARNING("HvyAttackWorker::Register #"<<Id()<<" failed as no slrqs!!");
		return -1;
	}

	slrq = slrqs.begin()->second;

	return performRes;
}

int HvyAttackWorker::Slrq(CURL* curl, const stringstream& body, const string& slrq, bool& isattacking)
{
	const int MaxTry = 70;

	const string curPage = "selSlrq_510100_1.aspx";
	const string nextPage = "selSlrqIn.aspx";		
	const string url = Host + ":" + ItoStr(_port) +"/" + curPage;
	const string dataFields = GetViewState(body) 
		+ "&lSldd=510104%3A%E4%B8%89%E5%88%86%E5%B1%80"
		+ "&dSlrq=" + slrq
		+ "&butSelect=+%E6%9F%A5+%E8%AF%A2+"
		+ "&" + GetEventValidation(body);

	time_t start = time(0);

	stringstream body2;
	int performRes = 0;
	int tryCounter = 0;
	while (isattacking && ++tryCounter<MaxTry && 0 != (performRes = Utility::PerformStep(curl, url, dataFields, body2)) && CAN_RE_REQUEST(performRes))
	{
		//LOGWARNING("HvyAttackWorker::Slrq #"<<Id()<<" failed errno #"<<performRes<<" at PerformStep 6 at try "<<tryCounter);
		Thread::SleepMs(100);
	}

	time_t end = time(0);

	APPENDMSGEX("HvyAttackWorker #"<<Id()<<" Slrq ended "<<end-start<<"s.", 1);

	if (!Utility::FileContain(body2, nextPage))
	{
		DumpFile(body, "dump_step_"+ItoStr(Id())+"_6_nonextpage.txt");
		return -1;
	}

	return performRes;
}

int HvyAttackWorker::MultiPerform(void)
{
	const int HANDLECOUNT = 300;

	static Mutex handlesInUse;
	static CURL* handles[HANDLECOUNT] = {NULL};
	CURLM* multi_handle = NULL;

	static const string method = "main.aspx";
	static const string queryStr = string("?str=510100")
		+ "|%E5%B7%9DA" 
		+ "|%E6%88%90%E9%83%BD%E5%B8%82%E5%85%AC%E5%AE%89%E5%B1%80%E4%BA%A4%E9%80%9A%E8%AD%A6%E5%AF%9F%E6%94%AF%E9%98%9F%E8%BD%A6%E8%BE%86%E7%AE%A1%E7%90%86%E6%89%80";
	static const string url = Host + ":" + ItoStr(_port) +"/" + method + queryStr;

	int still_running; /* keep number of running handles */ 
	int i;

	CURLMsg *msg; /* for picking up messages with the transfer status */ 
	int msgs_left; /* how many messages are left */ 

	//FILE* nu = fopen("/dev/null", "w");
	//assert(nu);

	/* Allocate one CURL handle per transfer */ 
	if (handles[HANDLECOUNT-1] == NULL)
	{
		Utility::Lock lock(&handlesInUse);
		if (handles[HANDLECOUNT - 1] == NULL)
		{
			for (i=0; i<HANDLECOUNT; i++)
			{
				//handles[i] = curl_easy_init();
				handles[i] = Utility::NewCURL();

				curl_easy_setopt(handles[i], CURLOPT_WRITEFUNCTION, Utility::write_data);
				curl_easy_setopt(handles[i], CURLOPT_WRITEDATA, NULL);

				curl_easy_setopt(handles[i], CURLOPT_URL, url.c_str());
			}
		}
	}

	/* init a multi stack */ 
	multi_handle = curl_multi_init();

	/* add the individual transfers */ 
	for (i=0; i<HANDLECOUNT; i++)
		curl_multi_add_handle(multi_handle, handles[i]);

	/* we start some action by calling perform right away */ 
	curl_multi_perform(multi_handle, &still_running);

	do {
		struct timeval timeout;
		int rc; /* select() return code */ 

		fd_set fdread;
		fd_set fdwrite;
		fd_set fdexcep;
		int maxfd = -1;

		long curl_timeo = -1;

		FD_ZERO(&fdread);
		FD_ZERO(&fdwrite);
		FD_ZERO(&fdexcep);

		/* set a suitable timeout to play around with */ 
		timeout.tv_sec = 1;
		timeout.tv_usec = 0;

		curl_multi_timeout(multi_handle, &curl_timeo);
		if(curl_timeo >= 0) {
			timeout.tv_sec = curl_timeo / 1000;
			if(timeout.tv_sec > 1)
				timeout.tv_sec = 1;
			else
				timeout.tv_usec = (curl_timeo % 1000) * 1000;
		}

		/* get file descriptors from the transfers */ 
		curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);

		/* In a real-world program you OF COURSE check the return code of the
		function calls.  On success, the value of maxfd is guaranteed to be
		greater or equal than -1.  We call select(maxfd + 1, ...), specially in
		case of (maxfd == -1), we call select(0, ...), which is basically equal
		to sleep. */ 

		rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);

		switch(rc) {
							case -1:	/* select error */ 
								break;
							case 0: /* timeout */ 
							default: /* action */ 
								curl_multi_perform(multi_handle, &still_running);
								APPENDMSGEX(" still running "<<still_running<<"...", 1);
								break;
		}
	} while(still_running);

	/* See how the transfers went */ 
	while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
		if (msg->msg == CURLMSG_DONE) {
			int idx, found = 0;
			/* Find out which handle this message is about */ 			for (idx=0; idx<HANDLECOUNT; idx++) {				found = (msg->easy_handle == handles[idx]);				if(found)				{					APPENDMSGEX("Curl "<<idx<<" finished:"<<msg->data.result<<".", 1);					break;				}
			}
		}
	}

	curl_multi_cleanup(multi_handle);

	/* Free the CURL handles */ 
	//for (i=0; i<HANDLECOUNT; i++)
	//    Utility::CleanupCurl(handles[i]);

	//fclose(nu);

	return 0;
}
